package main

import (
	"fmt"
	"git.inke.cn/baiying/slove/server/games/cell/cast"
	"git.inke.cn/bpc/server/dt/card/card_aggregation/models"
	"github.com/gorilla/schema"
	"net/url"
	"sync"
	"time"
)

//获取map里没有的key，会返回一个默认值
func main() {
	test4()
}

//测试map经过函数操作后值是否被改变了
func Test5() {
	mp := map[string]interface{}{}
	changeMap(mp)
	fmt.Println(mp)
}
func changeMap(mp map[string]interface{}) {
	mp["str"] = "str"
}
//测试通过携程是否可以对map进行操作：可以
func test4() {
	mp := map[string]interface{}{}
	for i := 0; i < 10; i++ {
		go func(i int, mp map[string]interface{}) {
			mp[cast.ToString(i)] = i
		}(i, mp)
	}
	fmt.Println(mp)git
	time.Sleep(3 * time.Second)
}

type themeRetInfoCard map[int]*models.HotRetCard

func TestDelete() {
	var mp = make(themeRetInfoCard, 3)
	mp2 := &mp
	mp[1] = &models.HotRetCard{}
	mp[2] = &models.HotRetCard{}
	delete(*mp2, 1)
	fmt.Println(mp2)
}

//测试指针map是否可以赋值
func Test3() {
	n := make(map[int]int)
	var m = &n
	mItem := *m
	mItem[1] = 1
	m = &mItem
	fmt.Println(m)
}

//测试map类型
func test1(mp map[int]string) {
	mp[3] = "222"

	_, ok1 := mp[3]
	fmt.Println(ok1)

	_, ok2 := mp[31]
	fmt.Println(ok2)
}

type Person struct {
	Name string `json:"name"`
}

//测试map中没值的话会报什么错误
func TestNoValue() {
	mp := map[string]Person{}
	s := mp["sss"].Name
	fmt.Println(fmt.Sprintf("%v111", s))
}

//数组为nil的时候可以塞进去item
func GenMap() {
	var m []map[int]string
	m = append(m, map[int]string{1: "s"})
}

var (
	once    sync.Once
	encoder *schema.Encoder
)

func initEncoder() {
	encoder = schema.NewEncoder()
}
func QueryMap(typ interface{}) map[string]string {
	once.Do(initEncoder)
	v := url.Values{}
	if err := encoder.Encode(typ, v); err != nil {
		return nil
	}

	m := make(map[string]string)
	for k := range v {
		m[k] = v.Get(k)
	}
	return m
}
